<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body>
<form class="layui-form layui-form-pane" id="operatePage">
    <div class="page-title">
        <div class="page-title-con">
            <span>证书下载</span><div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">下载</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
    <div class="main-container">
        <div class="block-first">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <table class="layui-table" lay-size="sm">
                            <thead>
                            <tr>
                                <th>人员名称</th>
                                <th>证书名称</th>
                                <th>附件</th>
                                <th>选择</th>
                            </tr>
                            </thead>
                            <tbody class="short_table_tbody">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id');

    layui.use(['laytp', 'form'], function() {
        var form = layui.form;

        facade.ajax({type:'get', url:ask_cert_use_down, data:{id:param_id}}).done(function(res){
            if(res.code === 2000){
                var data = res.data;
                var str = '';
                for(let i=0; i<data.length; i++){
                    if(data[i].len > 0){
                        var second_data = data[i].children;
                        str += '<tr>';
                        str += '<td rowspan="' + data[i].len + '">' + data[i].name + '</td>';
                        if(second_data[0].len == 0){
                            str += '<td rowspan="' + 1 + '">' + second_data[0].name + '</td>';
                        }else{
                            str += '<td rowspan="' + second_data[0].len + '">' + second_data[0].name + '</td>';
                        }
                        if(second_data[0].len > 0){
                            str += '<td>' + second_data[0].children[0].name + '</td>';
                            str += '<td style="width: 40px;"><input type="checkbox" value="'+second_data[0].children[0].id+'" lay-skin="primary"></td>';
                        }else{
                            str += '<td></td>';
                            str += '<td style="width: 40px;"></td>';
                        }
                        str += '</tr>';
                        if(second_data.length > 1) {// 多个二级
                            for (let j = 1; j < second_data.length; j++) {
                                var three_data = second_data[j].children;
                                str += '<tr>';
                                if(second_data[j].len == 0){
                                    str += '<td rowspan="' + 1 + '">' + second_data[j].name + '</td>';
                                }else{
                                    str += '<td rowspan="' + second_data[j].len + '">' + second_data[j].name + '</td>';
                                }
                                if(three_data.length > 0){
                                    str += '<td>' + three_data[0].name + '</td>';
                                    str += '<td style="width: 40px;"><input type="checkbox" value="'+three_data[0].id+'" lay-skin="primary"></td>';
                                }else{
                                    str += '<td></td>';
                                    str += '<td style="width: 40px;"></td>';
                                }
                                str += '</tr>';

                                if (second_data[j].children.length > 1) {
                                    for (let z = 1; z < three_data.length; z++) {
                                        str += '<tr>';
                                        str += '<td>' + three_data[z].name + '</td>';
                                        str += '<td style="width: 40px;"><input type="checkbox" value="'+three_data[z].id+'" lay-skin="primary"></td>';
                                        str += '</tr>';
                                    }
                                }
                            }
                        }else{
                            var three_data = second_data[0].children;
                            for (let z = 1; z < three_data.length; z++) {
                                str += '<tr>';
                                str += '<td>' + three_data[z].name + '</td>';
                                str += '<td style="width: 40px;"><input type="checkbox" value="'+three_data[z].id+'" lay-skin="primary"></td>';
                                str += '</tr>';
                            }
                        }
                    }else{
                        if(data[i].children.length > 0){
                            // str += '<tr>';
                            // str += '<td rowspan="'+data[i].children.length+'">'+data[i].name+'</td>';
                            // str += '<td></td>';
                            // str += '<td></td>';
                            // str += '<td></td>';
                            // str += '</tr>';
                            for(var x=0;x<data[i].children.length;x++){
                                str += '<tr>';
                                if(x == 0){
                                    str += '<td rowspan="'+data[i].children.length+'">'+data[i].name+'</td>';
                                }
                                str += '<td>'+data[i].children[x].name+'</td>';
                                str += '<td></td>';
                                str += '<td></td>';
                                str += '</tr>';
                            }
                        }
                    }
                }

                $('.short_table_tbody').html(str);
                render_from();
            }else{
                layer.msg(res.message);
            }
        });

        form.on('submit(add)', function(data){
            var ids = [''];
            $('input[type="checkbox"]').each(function (i) {
                if($('input[type="checkbox"]').eq(i).prop('checked')){
                    let id = $(this).val();
                    ids.push(id);
                }
            });
            if(ids.length === 1){
                facade.error('请选择要下载的附件'); return false;
            }
            data.field.ids = ids;
            data.field.id = param_id;
            facade.ajax({url: ask_cert_use_down, data: data.field}).done(function(res){
                if (res.code === 2000) {
                    down_file(res.data);
                    setTimeout((res)=>{
                        window.parent.location.reload();
                    }, 2000)
                }
            });

            return false;
        });
    });
</script>
</body>
</html>
